/**
 * This file is part of 1genia trampoline
 * Copyright (C) 2007 1genia (contact@1genia.com)
 *
 * This library is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; version 3 of the License. 
 *
 * This library is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details. 
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; see the file COPYING.TXT.  If not,
 * write to the Free Software Foundation, Inc., 51 Franklin Street,
 * Fifth Floor, Boston, MA 02110-1301, USA. 
 **/
package com.genia.toolbox.web.gwt.form.server.service.impl;

import java.util.ArrayList;
import java.util.List;

import com.genia.toolbox.basics.exception.BundledException;
import com.genia.toolbox.web.gwt.basics.client.i18n.GwtI18nMessage;
import com.genia.toolbox.web.gwt.basics.client.oracle.SimpleSuggestion;
import com.genia.toolbox.web.gwt.form.client.form.Form;
import com.genia.toolbox.web.gwt.form.client.form.impl.SubFormMultipleItemImpl;
import com.genia.toolbox.web.gwt.form.client.form.impl.SubFormPolicyImpl;
import com.genia.toolbox.web.gwt.form.client.form.impl.SubFormSimpleItemImpl;
import com.genia.toolbox.web.gwt.form.client.form.impl.TextBoxItemImpl;
import com.genia.toolbox.web.gwt.form.client.form.impl.VerticalFormImpl;
import com.genia.toolbox.web.gwt.form.client.validator.impl.RegularExpressionValidator;
import com.genia.toolbox.web.gwt.form.client.value.DataIdentifier;
import com.genia.toolbox.web.gwt.form.client.value.FormValues;
import com.genia.toolbox.web.gwt.form.client.value.impl.FormSimpleValueImpl;
import com.genia.toolbox.web.gwt.form.client.value.impl.FormValuesImpl;
import com.genia.toolbox.web.gwt.form.server.provider.FormProvider;

public class Form1
    implements FormProvider
{

  /**
   * the identifier of the form.
   */
  public static final String FORM_IDENTIFIER = "form1";

  private static Form form = null;

  private static final List<FormValues> formValues = new ArrayList<FormValues>();



  public String getDisplayString(final String fieldName, final String value)
  {
    if (value == null) {
      return null;
    }
    return Form2.formValues.get(Integer.parseInt(value)).getFormSimpleValue("name").getValue();
  }



  /**
   * returns the form handled by this object.
   * 
   * @return the form handled by this object
   * @throws BundledException
   *           when an error occurred
   * @see com.genia.toolbox.web.gwt.form.server.provider.FormProvider#getForm(String)
   */
  public Form getForm(DataIdentifier dataIdentifier)
  {
    if (form == null) {
      final SubFormPolicyImpl policy = new SubFormPolicyImpl();
      policy.setCanCreate(true);
      policy.setCanUpdate(true);

      final VerticalFormImpl vForm = new VerticalFormImpl();
      final TextBoxItemImpl tBox = new TextBoxItemImpl();
      tBox.setName("name");
      tBox.setLabelKey(new GwtI18nMessage("Name"));
      RegularExpressionValidator rev = new RegularExpressionValidator();
      rev.setRegularExpression("^..*$");
      tBox.setValidator(rev);
      vForm.addForm(tBox);

      final SubFormSimpleItemImpl ssForm = new SubFormSimpleItemImpl();
      ssForm.setName("master");
      ssForm.setLabelKey(new GwtI18nMessage("Master"));
      ssForm.setSubFormIdentifier("form2");
      ssForm.setSubFormPolicy(policy);
      vForm.addForm(ssForm);

      final SubFormMultipleItemImpl smForm = new SubFormMultipleItemImpl();
      smForm.setName("masters");
      smForm.setLabelKey(new GwtI18nMessage("Masters"));
      smForm.setPopup(true);
      smForm.setSortable(true);
      smForm.setSortButtonsDispositionVertical(true);
      smForm.setSubFormIdentifier("form2");
      smForm.setSubFormPolicy(policy);
      vForm.addForm(smForm);

      form = vForm;
    }
    return form;
  }



  public String getFormIdentifier()
  {
    return FORM_IDENTIFIER;
  }



  /**
   * retrieve the initial value of the object that is edited.
   * 
   * @param dataIdentifier
   *          the data identifier
   * @return the initial value of the object that is edited
   * @throws BundledException
   *           when an error occurred
   * @see com.genia.toolbox.web.gwt.form.server.provider.FormProvider#getInitialValue(java.lang.String)
   */
  public FormValues getInitialValue(final DataIdentifier dataIdentifier)
  {
    if (dataIdentifier.getIdentifier() == null) {
      FormValuesImpl defaultValues = new FormValuesImpl();
      defaultValues.setFormIdentifier(getFormIdentifier());
      FormSimpleValueImpl fv;

      fv = new FormSimpleValueImpl();
      fv.setValue(null);
      fv.setName("name");
      defaultValues.addFormSimpleValue(fv);

      fv = new FormSimpleValueImpl();
      fv.setValue(null);
      fv.setName("master");
      defaultValues.addFormSimpleValue(fv);

      fv = new FormSimpleValueImpl();
      fv.setValues(new ArrayList<String>());
      fv.setName("masters");
      defaultValues.addFormSimpleValue(fv);

      defaultValues.setDataIdentifier(dataIdentifier);
      return defaultValues;
    }

    final Integer index = Integer.valueOf(dataIdentifier.getIdentifier());
    return formValues.get(index);
  }



  /*
   * (non-Javadoc)
   * @see
   * com.genia.toolbox.web.gwt.form.server.provider.FormProvider#getSuggestions
   * (java.lang.String, java.lang.String, int)
   */
  public List<SimpleSuggestion> getSuggestions(DataIdentifier dataIdentifier, final String fieldName, final String query, int limit)
  {
    final List<SimpleSuggestion> res = new ArrayList<SimpleSuggestion>();
    int index = 0;
    for (FormValues fv : Form2.formValues) {
      if (fv.getFormSimpleValue("name").getValue().toLowerCase().contains(query.toLowerCase())) {
        limit--;
        final SimpleSuggestion sg = new SimpleSuggestion();
        sg.setDisplayString(fv.getFormSimpleValue("name").getValue());
        sg.setValue(String.valueOf(index));
        res.add(sg);
        if (limit <= 0) {
          break;
        }
      }
      index++;
    }
    return res;
  }



  /*
   * (non-Javadoc)
   * @see
   * com.genia.toolbox.web.gwt.form.server.provider.FormProvider#saveForm(com
   * .genia.toolbox.web.gwt.form.client.value.FormValues)
   */
  public FormValues saveForm(final FormValues newValues)
  {
    if (newValues.getDataIdentifier().getIdentifier() == null) {
      formValues.add(newValues);
      newValues.getDataIdentifier().setIdentifier(formValues.size() - 1);
    }
    else {
      formValues.set(Integer.parseInt(newValues.getDataIdentifier().getIdentifier()), newValues);
    }
    return newValues;
  }

}
